local sk__yingshi = fk.CreateSkill {

  name = "sk__yingshi",

  tags = { Skill.Compulsory, },

}



sk__yingshi:addEffect(fk.AfterDrawInitialCards, {
  name = "sk__yingshi",
  anim_type = "special",
  can_trigger = function(self, event, target, player, data)
    if player:hasSkill(sk__yingshi.name) then
      return target == player
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local mark = player:getTableMark(sk__yingshi.name)
    local choice = room:askForChoice(player, Fk:getAllCardNames("b", true), sk__yingshi.name, "#sk__yingshi-invoke")
    local cards = room:getCardsFromPileByRule(choice, 1, "drawPile")
    if #cards > 0 then
      room:moveCardTo(cards, Card.PlayerHand, player, fk.ReasonJustMove, sk__yingshi.name, nil, true, player)
    end
    if player.dead then return end
    for _, id in ipairs(player:getCardIds("h")) do
      local card = Fk:getCardById(id)
      if card.type == Card.TypeBasic then
        table.insertIfNeed(mark, id)
        room:setPlayerMark(player, sk__yingshi.name, mark)
        room:setCardMark(card, "@@sk__yingshi", 1)
      end
    end
  end,
})
sk__yingshi:addEffect(fk.AfterCardsMove, {
  name = "sk__yingshi",
  anim_type = "special",
  can_trigger = function(self, event, target, player, data)
    if player:hasSkill(sk__yingshi.name) then
      local from, cards = 0, {}
      for _, move in ipairs(data) do
        if move.from and move.from ~= player and move.to == player and move.toArea == Player.Hand and
            not table.contains(player:getTableMark("sk__yingshi-turn"), move.from) then
          for _, info in ipairs(move.moveInfo) do
            if info.fromArea == Card.PlayerHand then
              from = move.from.id
              table.insert(cards, info.cardId)
            end
          end
        end
      end
      cards = player.room.logic:moveCardsHoldingAreaCheck(cards)
      if #cards > 0 and table.every(cards, function(id)
            return Fk:getCardById(id).type == Card.TypeBasic
          end) and table.find(cards, function(id)
            return not table.contains(player:getTableMark(sk__yingshi.name), id)
          end) then
        event:setCostData(self, { from, cards })
        return true
      end
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local mark = player:getTableMark(sk__yingshi.name)
    local record = player:getTableMark("sk__yingshi-turn")
    table.insert(record, event:getCostData(self)[1])
    room:setPlayerMark(player, "sk__yingshi-turn", record)
    for _, id in ipairs(event:getCostData(self)[2]) do
      table.insertIfNeed(mark, id)
      room:setPlayerMark(player, sk__yingshi.name, mark)
      room:setCardMark(Fk:getCardById(id), "@@sk__yingshi", 1)
    end
  end,
})
sk__yingshi:addEffect(fk.TurnEnd, {
  name = "sk__yingshi",
  anim_type = "special",
  can_trigger = function(self, event, target, player, data)
    if player:hasSkill(sk__yingshi.name) then
      return table.find(player:getTableMark(sk__yingshi.name), function(id)
        return not table.contains(player:getCardIds("h"), id)
      end)
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local mark = player:getTableMark(sk__yingshi.name)
    local cards = table.filter(player:getTableMark(sk__yingshi.name), function(id)
      return not table.contains(player:getCardIds("h"), id)
    end)
    if #cards > 0 then
      room:moveCardTo(cards, Card.PlayerHand, player, fk.ReasonPrey, sk__yingshi.name, nil, true, player)
    end
  end,
})
sk__yingshi:addEffect(fk.CardUseFinished, {
  anim_type = "special",
  can_trigger = function(self, event, target, player, data)
    if player:hasSkill(sk__yingshi.name) then
      return target == player and U.isPureCard(data.card) and
          table.contains(player:getTableMark(sk__yingshi.name), data.card.id)
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local mark = player:getTableMark(sk__yingshi.name)
    player:drawCards(1, sk__yingshi.name)
  end,
})

sk__yingshi:addLoseEffect(function(self, player, is_death)
  local room = player.room
  local mark = player:getTableMark(sk__yingshi.name)
  for _, id in ipairs(mark) do
    if not table.find(room:getOtherPlayers(player), function(p)
          return table.contains(p:getTableMark(sk__yingshi.name), id)
        end) then
      room:setCardMark(Fk:getCardById(id), "@@sk__yingshi", 0)
    end
  end
  room:setPlayerMark(player, sk__yingshi.name, 0)
end)

sk__yingshi:addEffect(fk.Deathed, {
  can_refresh = function(self, event, target, player, data)
    if target == player then
      return player:hasSkill(self, true, true)
    end
  end,
  on_refresh = function(self, event, target, player, data)
    local room = player.room
    local mark = player:getTableMark(sk__yingshi.name)
    for _, id in ipairs(mark) do
      if not table.find(room:getOtherPlayers(player), function(p)
            return table.contains(p:getTableMark(sk__yingshi.name), id)
          end) then
        room:setCardMark(Fk:getCardById(id), "@@sk__yingshi", 0)
      end
    end
    room:setPlayerMark(player, sk__yingshi.name, 0)
  end,
})

sk__yingshi:addEffect("targetmod", {
  name = "#sk__yingshi_targetmod",
  --sk__yingshi,
  bypass_times = function(self, player, skill, scope, card, to)
    return player:hasSkill(sk__yingshi.name) and player:usedSkillTimes("sk__yingshi", Player.HistoryGame) > 0 and
        card and card:getMark("@@sk__yingshi") > 0
  end,
  bypass_distances = function(self, player, skill, card, to)
    return player:hasSkill(sk__yingshi.name) and player:usedSkillTimes("sk__yingshi", Player.HistoryGame) > 0 and
        card and card:getMark("@@sk__yingshi") > 0
  end,
})

return sk__yingshi
